package dao;

import common.Group;
import common.User;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class GetFriends {
    /**
     * 根据自己的ID来查询朋友的昵称并添加到列表上面
     */
    public ArrayList<User> getFriends(User u) {
        ArrayList<User> friends= new ArrayList<User>();
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn= DriverManager.getConnection(url,user,password);
            String sql1="select * from friendlist where user_id = ? and statement = ?";
            String sql2="select * from friendlist where friend_id = ? and statement = ?";
            PreparedStatement presta = conn.prepareStatement(sql1);
            presta.setString(1, u.getId());
            presta.setInt(2,1);
            res=presta.executeQuery();
            while(res.next()) {
                String fid=res.getString("friend_id");
                //System.out.println(fid);
                //System.out.println("11111");
                User newf=new User();
                newf.setId(fid);
                friends.add(newf);
            }
            //System.out.println(friends.size());
            presta = conn.prepareStatement(sql2);
            presta.setString(1, u.getId());
            presta.setInt(2,1);
            res=presta.executeQuery();
            while(res.next()) {
                String fid=res.getString("user_id");
                //System.out.println(fid);
                //System.out.println("11111");
                User newf=new User();
                newf.setId(fid);
                friends.add(newf);
            }
            //System.out.println(friends.size());
            return friends;
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return friends;
    }

    //获取我创建的群聊
    public ArrayList<Group> getGroups(Group u) {
        ArrayList<Group> groups= new ArrayList<Group>();
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn=DriverManager.getConnection(url,user,password);
            String sql1="select * from grouplist where member_id = ? and statement = ?";
            PreparedStatement presta = conn.prepareStatement(sql1);
            presta.setString(1, u.getuserid());
            presta.setInt(2,2);
            res=presta.executeQuery();
            while(res.next()) {
                String fid=res.getString("group_id");
                Group newf=new Group();
                newf.setuserid(u.getuserid());
                newf.setGroupid(fid);
                groups.add(newf);
            }
            return groups;
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return groups;
    }

    public ArrayList<Group> getotherGroups(Group u) {
        ArrayList<Group> groups= new ArrayList<Group>();
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn=DriverManager.getConnection(url,user,password);
            String sql1="select * from grouplist where member_id = ? and statement = ?";
            PreparedStatement presta = conn.prepareStatement(sql1);
            presta.setString(1, u.getuserid());
            presta.setInt(2,1);
            res=presta.executeQuery();
            while(res.next()) {
                String fid=res.getString("group_id");
                Group newf=new Group();
                newf.setGroupid(fid);
                groups.add(newf);
            }
            return groups;
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return groups;
    }
}
